package io.github.wx.web;

import com.alibaba.fastjson.JSONObject;
import io.github.wx.common.controller.BaseController;
import io.github.wx.common.geetest.GeetestLib;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

/**
 * Created by jie on 2017/4/2.
 * 主页跳转
 */
public class IndexController extends BaseController {
    private static final String geetest_id = "bff8172bd7fe03c1c3217b1b3cc49e3a";
    private static final String geetest_key = "ae792fcf82cfd86c63e1b250466115a7";

    /**
     * 主页
     */
    public void index() {
        render("index.html");
    }

    public void login() {
        GeetestLib gtSdk = new GeetestLib(geetest_id, geetest_key, true);
        gtSdk.preProcess();
        String responseStr = gtSdk.getResponseStr();
        setAttr("gtdata", JSONObject.parse(responseStr));
        render("login.html");
    }

    public void userLogin() {
        GeetestLib gtSdk = new GeetestLib(geetest_id, geetest_key, true);
        String challenge = getPara(GeetestLib.fn_geetest_challenge);
        String validate = getPara(GeetestLib.fn_geetest_validate);
        String seccode = getPara(GeetestLib.fn_geetest_seccode);
        int result = gtSdk.enhencedValidateRequest(challenge, validate, seccode);

        if (1 == result) { /*验证成功*/
            Subject currentUser = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(
                    "123", "123");
            currentUser.login(token);
            renderFailed();
        } else {/*验证失败*/
            renderFailed();
        }
    }

}
